package com.pi.pro.xcmg.am.was.edi.wcs.common;


import com.pi.common.utils.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.common.vo.act.MoveChangeDto;
import com.pi.pro.xcmg.am.was.edi.wcs.dto.*;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.entity.TwtActTaskList;


public class WcsWasExchangeSupport {


    public static CommonResultData ok(){
        CommonResultData replyData=new CommonResultData();
        replyData.setCode(WcsWasXcmgExchangeConstants.RESULT_CODE_OK);
        replyData.setMsg("成功");
        return replyData;
    }

    public static CommonResultData feedback(){
        return feedback("");
    }

    public static CommonResultData feedback(String errorText){
        CommonResultData replyData=new CommonResultData();
        replyData.setCode(WcsWasXcmgExchangeConstants.RESULT_CODE_RETURN);
        if (StringUtils.isNullOrBlank(errorText)){
            errorText="异常回退";
        }
        replyData.setMsg(errorText);
        return replyData;
    }

    public static CommonResultData error(String errorText){
        CommonResultData replyData=new CommonResultData();
        replyData.setCode(WcsWasXcmgExchangeConstants.RESULT_CODE_ERROR);
        if (StringUtils.isNullOrBlank(errorText)){
            errorText="发生异常.";
        }
        replyData.setMsg(errorText);
        return replyData;
    }

    public static <T> boolean hasError(CommonResultData replyData){
        if (replyData==null){
            return true;
        }
        if (!WcsWasXcmgExchangeConstants.RESULT_CODE_OK.equalsIgnoreCase(replyData.getCode())){
            return true;
        }
        return false;
    }

    public static String errorText(CommonResultData replyData){
        String m="";
        if (replyData==null){
            m="未收到WCS反馈,请确认WCS已经开启.";
            return m;
        }
        if (!WcsWasXcmgExchangeConstants.RESULT_CODE_OK.equalsIgnoreCase(replyData.getCode())){
            m="下发WCS任务失败.";
            if (!StringUtils.isNullOrBlank(replyData.getMsg())){
                m=replyData.getMsg();
            }
        }
        return m;
    }


    public static String check(MoveRequestData requestData){
        String m = "";
        if (requestData==null){
            m="请求数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(requestData.getSiteCode())){
            m="请求数据无效,站点为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(requestData.getPalletCode())){
            m="请求数据无效,托盘码为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(requestData.getType())){
            m="请求数据无效,请求类型为空.";
            return m;
        }
        if (WcsWasXcmgExchangeConstants.moveReqTypes.containsKey(requestData.getType())){
            m="请求数据无效,请求类型无效:[2:空托入库; 1:扫码入库;  0:拣选回库].当前:"+requestData.getType();
            return m;
        }
        if (requestData.getHeight()==null){
            m="请求数据无效,高度为空.";
            return m;
        }
        if (requestData.getHeight()<=0){
            m="请求数据无效,高度无效,必须大于0.当前:"+String.valueOf(requestData.getHeight());
            return m;
        }
        return m;
    }

    public static String check(InboundCheckData checkData){
        String m = "";

        if (checkData==null){
            m="状态数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(checkData.getSiteCode())){
            m="状态数据无效,站点为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(checkData.getBizType())){
            m="状态数据无效,状态类型为空.";
            return m;
        }
        if (!WcsWasXcmgExchangeConstants.moveCheckTypes.containsKey(checkData.getBizType())){
            m="状态数据无效,业务类型无效:[1:入库拍按钮传; 3:称重和外形检测传].当前:"+checkData.getBizType();
            return m;
        }
        return m;
    }

    public static String check(TaskReportData reportData){
        String m = "";

        if (reportData==null){
            m="任务结果数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(reportData.getCode())){
            m="任务结果数据无效,任务号为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(reportData.getToSite())){
            m="任务结果数据无效,到达站点为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(reportData.getStatus())){
            m="任务结果数据无效,状态为空.";
            return m;
        }
        if (!WcsWasXcmgExchangeConstants.taskChangeStatus.containsKey(reportData.getStatus())){
            m="任务结果数据无效,业务类型无效:[1:任务开始;  2:任务完成;  3:任务取消.].当前:"+reportData.getStatus();
            return m;
        }

        return m;
    }

    public static String check(StationReportData reportData){
        String m = "";

        if (reportData==null){
            m="状态数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(reportData.getCode())){
            m="状态数据无效,站点为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(reportData.getStatus())){
            m="状态数据无效,状态为空.";
            return m;
        }
        if (!WcsWasXcmgExchangeConstants.stationLiveStatus.containsKey(reportData.getStatus())){
            m="状态数据无效,状态值无效:[1:空闲;  2:有货;  4:停用.].当前:"+reportData.getStatus();
            return m;
        }

        return m;
    }

    public static String check(EPalletRequestData requestData){
        String m = "";

        if (requestData==null){
            m="呼叫空托数据为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(requestData.getCode())){
            m="呼叫空托数据无效,站点为空.";
            return m;
        }
        if (StringUtils.isNullOrBlank(requestData.getType())){
            m="呼叫空托数据无效,请求类型为空.";
            return m;
        }
        if (!WcsWasXcmgExchangeConstants.containerType.containsKey(requestData.getType())){
            m="呼叫空托数据无效,请求类型无效:[1:铁料箱;  2:铁托盘.].当前:"+requestData.getType();
            return m;
        }
        if (requestData.getQuantity()==null){
            m="请求数据无效,数量为空.";
            return m;
        }
        /**
        if (requestData.getQuantity()<=0){
            m="请求数据无效,数量无效,必须大于0.当前:"+String.valueOf(requestData.getQuantity());
            return m;
        }
         **/

        return m;
    }

    public final static MoveChangeDto translateTo(MoveRequestData moveRequestData){
        MoveChangeDto moveChangeDto = null;
        if (moveRequestData==null){
            return moveChangeDto;
        }
        moveChangeDto = new MoveChangeDto();
        moveChangeDto.setContainerNumber(moveRequestData.getPalletCode());
        moveChangeDto.setLocationCode(moveRequestData.getSiteCode().trim());
        moveChangeDto.setChangeType(moveRequestData.getType().trim());
        return moveChangeDto;
    }

    public final static TransportTaskData translateTo(TwtActTaskList task){
        TransportTaskData transportTask = null;
        if (task==null){
            return transportTask;
        }
        transportTask = new TransportTaskData();
        transportTask.setCode(task.getNumber());
        if (WmsConstants.TASK_TYPE_PUTAWAY.equalsIgnoreCase(task.getType())){
            transportTask.setType(WcsWasXcmgExchangeConstants.TASK_TYPE_TEXT_INBOUND);
        }
        if (WmsConstants.TASK_TYPE_PICK.equalsIgnoreCase(task.getType())){
            transportTask.setType(WcsWasXcmgExchangeConstants.TASK_TYPE_TEXT_OUTBOUND);
        }
        if (WmsConstants.TASK_TYPE_MOVE.equalsIgnoreCase(task.getType())){
            transportTask.setType(WcsWasXcmgExchangeConstants.TASK_TYPE_TEXT_MOVE);
        }
        transportTask.setPriority(WcsWasXcmgExchangeConstants.TASK_PRIORITY_NORMAL);
        if ((!StringUtils.isNullOrBlank(task.getTaskPrior())) && (task.getTaskPrior().compareToIgnoreCase(WmsConstants.TASK_PRIOR_NORMAL)<0)){
            transportTask.setPriority(WcsWasXcmgExchangeConstants.TASK_PRIORITY_HIGH);
        }
        transportTask.setContainerCode(task.getFromContainNumber());
        transportTask.setFromSite(task.getFromLocSeq());
        transportTask.setToSite(task.getToLocSeq());
        transportTask.setOptionSite(taskOptionValueOf(task));
        transportTask.setProperty(WcsWasXcmgExchangeConstants.WORK_WITH_LOAD);

        return transportTask;
    }

    public final static String taskOptionValueOf(TwtActTaskList task){
        String val="";
        if (task==null){
            return val;
        }
        if (task.getTaskFeature()!=null){
            val=task.getTaskFeature().trim();
        }
        return val;
    }

    public final static String taskOptionValue(TwtActTaskList task, String options){
        String m="";
        if (task==null){
            return m;
        }
        if (options==null){
            return m;
        }
        if (StringUtils.isNullOrBlank(task.getTaskFeature())){
            task.setTaskFeature(options.trim());
        }
        return m;
    }
}
